package com.iris.smartclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iris.smartclass.mapper.SituationMapper;
import com.iris.smartclass.mapper.StudentMapper;
import com.iris.smartclass.model.entity.Situation;
import com.iris.smartclass.model.entity.Student;
import com.iris.smartclass.model.entity.User;
import com.iris.smartclass.model.vo.SituationVO;
import com.iris.smartclass.service.SituationService;
import com.iris.smartclass.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* @author 孙鸣
* @description 针对表【situation(座位表)】的数据库操作Service实现
* @createDate 2024-12-31 09:20:33
*/
@Service
public class SituationServiceImpl extends ServiceImpl<SituationMapper, Situation>
    implements SituationService {
    @Resource
    private SituationMapper situationMapper;

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private UserService userService;


    @Override
    public SituationVO getSituationVO(Long stId) {
        QueryWrapper<Situation> situationQueryWrapper = new QueryWrapper<>();
        situationQueryWrapper.eq("stId",stId);
        Situation situation = situationMapper.selectOne(situationQueryWrapper);
        String name = studentMapper.selectOne(new QueryWrapper<Student>().eq("id", stId)).getName();
        SituationVO situationVO = new SituationVO();
        BeanUtils.copyProperties(situation,situationVO);
        situationVO.setName(name);
        return situationVO;
    }

    @Override
    public List<SituationVO> getSituationVOList(HttpServletRequest request) {
        List<SituationVO> situationVOList = new ArrayList<>();
        User loginUser = userService.getLoginUser(request);
        Long uuid = loginUser.getId();
        QueryWrapper<Situation> situationQueryWrapper = new QueryWrapper<>();
        situationQueryWrapper.eq("uuid",uuid);
        List<Situation> situations = situationMapper.selectList(situationQueryWrapper);
        for (Situation situation : situations) {
            Long stId = situation.getStId();
            SituationVO situationVO = getSituationVO(stId);
            situationVOList.add(situationVO);
        }
        return situationVOList;
    }


}




